package com.lkd.handler;
import com.baomidou.mybatisplus.core.toolkit.CollectionUtils;
import com.lkd.business.MsgHandler;
import com.lkd.common.VMSystem;
import com.lkd.config.TopicConfig;
import com.lkd.contract.SupplyContract;
import com.lkd.emq.Topic;
import com.lkd.feign.VMService;
import com.lkd.http.vo.TaskDetailsViewModel;
import com.lkd.http.vo.TaskViewModel;
import com.lkd.service.TaskService;
import com.lkd.utils.JsonUtil;
import com.lkd.vo.VmVO;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Component;
import java.io.IOException;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;
import java.util.stream.Collectors;

/**
 *自动创建补货工单
 */
@Topic(TopicConfig.TASK_SUPPLY_TOPIC)
@Component
@Slf4j
public class SupplyMsgHandler implements MsgHandler {

    @Autowired
    private TaskService taskService;
    @Autowired
    private RedisTemplate redisTemplate;
    @Autowired
    private VMService vMService;


    @Override
    public void process(String jsonMsg) throws IOException {
        if (StringUtils.isEmpty(jsonMsg)) {
            return;
        }
        //解析emq传来的消息
        SupplyContract supplyContract = JsonUtil.getByJson(jsonMsg, SupplyContract.class);
        if(supplyContract == null || StringUtils.isEmpty(supplyContract.getInnerCode()) || CollectionUtils.isEmpty(supplyContract.getSupplyData())){
            return;
        }
        TaskViewModel taskVo = new TaskViewModel();
        taskVo.setCreateType(0);
        taskVo.setInnerCode(supplyContract.getInnerCode());
        taskVo.setProductType(VMSystem.TASK_TYPE_SUPPLY);
        taskVo.setDesc("自动补货工单");
        taskVo.setUserId(findMinTaskCountUser(taskVo));

        List<TaskDetailsViewModel> detailsViewModelList = supplyContract.getSupplyData().stream().map(supplyData -> {
            TaskDetailsViewModel taskDetailsViewModel = new TaskDetailsViewModel();
            taskDetailsViewModel.setChannelCode(supplyData.getChannelId());
            taskDetailsViewModel.setExpectCapacity(supplyData.getCapacity());
            taskDetailsViewModel.setSkuId(supplyData.getSkuId());
            taskDetailsViewModel.setSkuName(supplyData.getSkuName());
            taskDetailsViewModel.setSkuImage(supplyData.getSkuImage());
            return taskDetailsViewModel;
        }).collect(Collectors.toList());

        taskVo.setDetails(detailsViewModelList);
        taskService.saveTask(taskVo);
    }

    /**
     * 查询手上工单最少的用户信息
     * @param taskViewModel
     * @return
     */
    private Integer findMinTaskCountUser(TaskViewModel taskViewModel) {
        //任务执行时间
        String format = new SimpleDateFormat("yyyyMMdd").format(new Date());

        //获取任务执行区域
        VmVO vmInfo = vMService.getVMInfo(taskViewModel.getInnerCode());

        String roleCode = "1002"; //运营人员编号
        if (taskViewModel.getProductType() != VMSystem.TASK_TYPE_SUPPLY) {
            roleCode = "1003"; //运维人员编号
        }

        String key = VMSystem.REGION_TASK_KEY_PREF + format + "." + vmInfo.getRegionId() + "." + roleCode;

        /**
         *取出最上面的那个值,也就是最小的值
         */
        Integer userId = (Integer) redisTemplate.opsForZSet().range(key, 0, 1)
                .stream().findFirst().get();
        log.info("userid:{}",userId);
        return userId;
    }
}
